package server;

import model.UserRank;
import net.sf.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * UserRank: tum0r
 * Package Name: server
 * Time: 2019-01-28 Monday 16:10:48
 */
public class RankServer extends BaseServer {
    private LinkedList<UserRank> userRank;
    private UserRank[] userRanks;

    public LinkedList<UserRank> getUserRank() throws ParseException {
        userRank = new LinkedList<>();
        rank();
        Arrays.sort(userRanks, new UserRank());
        Collections.addAll(userRank, userRanks);
        return userRank;
    }

    private void rank() throws ParseException {
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select content from setting where name=?", new Object[]{"game"});
        JSONObject gameInfo = JSONObject.fromObject(temp.get(0).get("content").toString());
        String punish = gameInfo.getString("punish");
        String startTime = gameInfo.getString("start");
        temp = sql.query("select * from user where type=? and state=?", new Object[]{0, 1});
        int size = temp.size();
        userRanks = new UserRank[size];
        LinkedList<LinkedHashMap<String, Object>> recordTemp;
        LinkedHashMap<String, Object> recordTempItem;
        LinkedList<LinkedHashMap<String, Object>> recordAcceptTemp;
        LinkedHashMap<String, Object> recordNewTempItem;
        boolean flag;
        String userID;
        String username;
        int solveCount;
        int notSolveCount;
        String lastSubmitTime;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        long t1, t2;

        for (int i = 0; i < size; i++) {
            userID = temp.get(i).get("uuid").toString();
            username = temp.get(i).get("username").toString();
            recordTemp = sql.query("select subject_id, result, submit_time from record where result=? and user_id=? order by submit_time desc", new Object[]{"Accepted", userID});
            recordAcceptTemp = new LinkedList<>();
            notSolveCount = 0;
            for (int j = 0; j < recordTemp.size(); j++) {
                flag = true;
                recordTempItem = recordTemp.get(j);
                for (int k = 0; k < recordAcceptTemp.size(); k++) {
                    recordNewTempItem = recordAcceptTemp.get(k);
                    if (recordTempItem.get("subject_id").toString().equals(recordNewTempItem.get("subject_id").toString())) {
                        t1 = simpleDateFormat.parse(recordTempItem.get("submit_time").toString()).getTime();
                        t2 = simpleDateFormat.parse(recordNewTempItem.get("submit_time").toString()).getTime();
                        if (t1 < t2) {
                            recordAcceptTemp.remove(recordNewTempItem);
                            recordAcceptTemp.add(recordTempItem);
                        }
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    recordAcceptTemp.add(recordTempItem);
                }
            }
            solveCount = recordAcceptTemp.size();

            lastSubmitTime = "";
            if (solveCount > 0) {
                lastSubmitTime = recordAcceptTemp.get(0).get("submit_time").toString();
            }

            recordTemp = sql.query("select subject_id, result, submit_time from record where user_id=? order by submit_time desc", new Object[]{userID});
            if (recordTemp.size() > 0) {
                recordTempItem = recordTemp.get(0);
                String lastTime = (String) recordTempItem.get("submit_time");
                notSolveCount = sql.query("select * from record where result!=? and result!=? and user_id=? and submit_time<=?", new Object[]{"Accepted", "Punished", userID, lastTime}).size();
                notSolveCount += sql.query("select * from record where result=? and user_id=?", new Object[]{"Punished", userID}).size();
            }

            userRanks[i] = new UserRank(username, solveCount, notSolveCount, startTime, lastSubmitTime, punish);
            userRanks[i].setAllCount(sql.query("select * from record where user_id=?", new Object[]{userID}).size());
        }
    }
}
